<template>
  <div class="login-box">
    <div class="login-input-box center">
      <h1 class="t-center">信贷管理系统</h1>
      <div class="login-form">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="0px"
          class="demo-ruleForm"
        >
          <el-form-item prop="account">
            <el-input
              prefix-icon="el-icon-user-solid"
              type="text"
              v-model="ruleForm.account"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-s-order"
              type="password"
              v-model="ruleForm.password"
              autocomplete="off"
            />
          </el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/user";

export default {
  mounted(){
    this.enterKeyFn =(e)=>{
      if(e.key==='Enter'){
        // console.log("点击了回车")
        this.submitForm()
      }
    }
    window.addEventListener('keypress', this.enterKeyFn)
  },
  beforeDestroy(){
    window.removeEventListener('keypress',this.enterKeyFn)
  },
  data() {
    return {
      ruleForm: {
        account: "admin",
        password: "approve123456.",
      },
      rules: {
        account: [{ required: true, trigger: "blur", message: "请输入用户名" }],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let [err, res] = await /*pretty(*/ login(this.ruleForm /*{
            account: "admin",
            password: "approve123456.",
          }*/); /*)*/
          // .then((res) => {
          //   console.log("res", res);
          // })
          // .catch((err) => {
          //   console.log("err", err);
          // });
          if (err) {
            // 提示error
            // return alert("异常", err.message);
            return
          }
          // console.log("res", res, err);
          this.$router.push(this.$route.query.redirect|| '/dashboard')// todo:需要有更好的用户体验:通过用户之前访问url跳转
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login-box {
  width: 100%;
  height: 100%; /* 临时 */
  background: url("../assets/bg2.jpg");
  /* background-size: contain; 以图本身为基础，2张图撑满*/
  background-size: cover;
  /* 以容器为基础，变化图片大小撑满 */
}
.login-input-box {
  width: 700px;
  height: 370px;
  background: #fff;
  padding: 40px 40px 12px 12px;
  /* 变化盒模型:  */
  box-sizing: border-box;
}
.el-button {
  width: 100%;
}
.el-input {
  margin-bottom: 20px;
}
/* v-deep 组件的属性选择器 */
/* ::v-deep .el-input__inner {
    background-color: #e5e5e5;
} */
/* 最新推荐 */
:deep(.el-input__inner) {
  background-color: #e5e5e5;
}
</style>